package com.haixue.android.academy.adapter;

import android.academy.haixue.com.haixue.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.haixue.android.academy.domain.Goods;
import com.haixue.android.academy.domain.Subjects;
import com.haixue.android.academy.utils.BaseHolder;
import com.haixue.android.academy.view.tiku.TiikuDataView;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

/**
 * Created by ren on 2015/6/8 0008.
 */
public class GoodsAdapter extends BaseExpandableListAdapter{
    private final Context context;
    private List<Goods> datas = new ArrayList<Goods>();
    protected final LayoutInflater layoutInflater;

    public GoodsAdapter(Context context){
        this.context =context;
        layoutInflater = LayoutInflater.from(context);
    }
    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return datas.get(i).getSubjects().size();
    }

    @Override
    public Object getGroup(int i) {
        return datas.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return datas.get(i).getSubjects().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i*i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup) {
        GroupViewHolder holder;
        if (convertView != null) {
            holder = (GroupViewHolder) convertView.getTag();
        } else {
            convertView =layoutInflater.inflate(R.layout.item_goods, null);
            holder = new GroupViewHolder(context,convertView);
            convertView.setTag(holder);
        }

        Goods goods = datas.get(i);
        holder.setData(goods);
        return convertView;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View convertView, ViewGroup viewGroup) {
        ChildViewHolder holder;
        if (convertView != null) {
            holder = (ChildViewHolder) convertView.getTag();
        } else {
            convertView =layoutInflater.inflate(R.layout.item_subject, null);
            holder = new ChildViewHolder(context,convertView);
            convertView.setTag(holder);
        }

        Subjects subjects = datas.get(i).getSubjects().get(i1);
        holder.setData(subjects,datas.get(i).getGoodsKind());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    public void setData(List<Goods> data) {
        datas.clear();
        datas.addAll(data);
        notifyDataSetChanged();
    }

    static class GroupViewHolder extends BaseHolder {
        @InjectView(R.id.iv_label)
        ImageView iv_label;

        @InjectView(R.id.tv_title)
        TextView tv_title;

        @InjectView(R.id.tv_arrow)
        ImageView tv_arrow;

        public GroupViewHolder(Context context,View view) {
            super(context,view);
        }

         public void setData(Goods goods) {

             switch (goods.getGoodsKind()) {
                 case 0:

                     iv_label.setImageResource(R.drawable.goodskind_video);

                     break;

                 case 1:

                     iv_label.setImageResource(R.drawable.goodskind_live);

                     break;

                 case 2:

                     iv_label.setImageResource(R.drawable.goodskind_all);

                     break;

                 default:

                     iv_label.setImageResource(R.drawable.goodskind_video);

                     break;
             }

             tv_title.setText(goods.getGoodsName());

//             if (expand) {
//
//                 iv_arrow.setImageResource(R.drawable.goods_collapse);
//                 //			linearLayout.setBackgroundResource(R.drawable.goods_bg_collapse);
//             } else {
//
//                 iv_arrow.setImageResource(R.drawable.goods_expanded);
//                 //			linearLayout.setBackgroundResource(R.drawable.goods_bg_collapse);
//             }
         }
     }
    static class ChildViewHolder extends BaseHolder {
        @InjectView(R.id.imageView_video_cover)
        ImageView imageView_video_cover;

        @InjectView(R.id.textview_subject_name)
        TiikuDataView textview_subject_name;

        @InjectView(R.id.textview_time_info_begin)
        TextView textview_time_info_begin;

        @InjectView(R.id.textView_subject_seperator)
        TextView textView_subject_seperator;

        public ChildViewHolder(Context context,View view) {
            super(context,view);
        }

         public void setData(Subjects subject, int goodsKind) {
             switch (goodsKind) {
                 case 0:
                     imageView_video_cover.setImageResource(R.drawable.cover_good_kind_video);
                     break;

                 case 1:
                     imageView_video_cover.setImageResource(R.drawable.cover_good_kind_live);
                     break;

                 case 2:
                     imageView_video_cover.setImageResource(R.drawable.cover_good_kind_all);
                     break;

                 default:
                     imageView_video_cover.setImageResource(R.drawable.cover_good_kind_video);
                     break;
             }

             if (subject.getCatelogType() == 2) {
                 textview_time_info_begin.setVisibility(View.GONE);
             } else {
                 textview_time_info_begin.setText(getString(R.string.video_module_count, subject.getModuleCount()));
                 textview_time_info_begin.setVisibility(View.VISIBLE);
             }

//             if (subject.getLastUpdateTime() > LocalPreferencesDataCenter.getGoodLastViewTime(getContext(), subjectData.getId())) {
//
//                 // new
//                 int IMAGE = R.drawable.icon_new_xml;
//
//                 textview_subject_name.setText(subject.getSubjectName() + "   <img src=\"" + IMAGE + "\" />");
//
//             } else {
//                 textview_subject_name.setText(subject.getSubjectName());
//
//             }
             textview_subject_name.setText(subject.getSubjectName());
//             if (isLast) {
//                 textViewSeperator.setVisibility(View.GONE);
//                 setBackgroundResource(R.drawable.goods_bg_collapse);
//             } else {
//                 textViewSeperator.setVisibility(View.VISIBLE);
//                 setBackgroundColor(getResources().getColor(R.color.color_text_8));
//             }
         }
     }
}
